Ext.define('PJ.view.celaquestionnairemanage.IndividualChoiceList', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.celaIndividualChoiceList'],
    requires: ['PJ.view.celaquestionnairemanage.SelectQuestionsList'//从题库选取题目
               ],
  
    //单选题列表
    isHavePage: false,
    isPossibleLoad: false,
    
    initComponent: function(){
    	var me = this;
        me.columns = [{
            header: '调研题目',
            dataIndex: 't.questionName',
            flex: 1,
            minWidth: 120,
			renderer: function(value, meta, record){
                meta.style = "overflow: hidden;text-overflow: ellipsis;cursor: pointer;color: blue;";
                meta.tdAttr = 'data-qtip="' + value + '" questionId=' + record.get("questionId");
                return value;
            },
            listeners: {
                click: function(g, clm){
                    Ext.create('PJ.view.celabankmanagement.Edit', {
                        title: "查看题目详细信息",
                        isReadOnly:true,
                        grid: g,
                        isEdit: false,
                        notEdit: false,
                        questionId: clm.getAttribute("questionId")
                    }).show();
                }
            }
        }, {
            header: '是否必填',
            dataIndex: 't.isMust',
            renderer: function(value){
                if (value == 1) {
                    return "是";
                }
                else 
                    if (value == 2) {
                        return "否";
                    }
            },
            editor: {
                xtype: 'combo',
                queryMode: 'local',
                
                store: Ext.create("Ext.data.ArrayStore", {
                    fields: ['value', 'text'],
                    data: [[1, '是'], [2, '否']]
                }),
                
                valueField: 'value',
                displayField: 'text',
                editable: false,
                listeners: {
                	blur: function(combo){
                        var url = me.store.requiredUrl;
                        var value = combo.getValue();
                        var record = combo.up("grid").getSelectionModel().getLastSelected();
                        if(gj_onlineLearning_constant.loadType == "jsonp"){
                        	var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                        	    url: url,
                        	    extraParams: {
                        	    	isMust: value,
                        	    	questionnaireQuestionId: record.get("questionnaireQuestionId"),
									"springmvc.TOKEN":combo.token
                        	    },
                        	    loadType:gj_onlineLearning_constant.loadType
                        	});
                        	store.load(function(rec, operation, success){
                        		var json = {
                        				 data :operation.response.data,
                        				 msg:operation.response.msg
                        			};
                        	     if (success) {
                        	    	 combo.up("grid").store.load();
                        	     }else {
                        	    	 Orchid.alert('消息', '修改失败！', function(){
                                         combo.up("grid").store.load();
                                     });
                        	    }
                        	});
                        }else if(gj_onlineLearning_constant.loadType == "ajax"){
                        	Ext.Ajax.request({
                                url: url,
                                params: {
                                	isMust: value,
                                	questionnaireQuestionId: record.get("questionnaireQuestionId"),
									"springmvc.TOKEN":combo.token
                                },
                                success: function(response){
                                    var data = response.responseText;
                                    var json = Ext.decode(data, true);
                                    if (json.success) {
                                    	 combo.up("grid").store.load();
                                    }
                                    else {
                                        Orchid.alert('消息', '修改失败！', function(){
                                            combo.up("grid").store.load();
                                        });
                                    }
                                }
                            });
                        }
                    }
                }
            },
            width: 130
        },{
			header: '排序',
			dataIndex: 't.orderNum',
			width: 50,
			sortable: false,
			editor: {
				xtype: 'textfield',
				regexText: '请输入正整数',
				regex: /^[1-9]*[0-9][0-9]*$/,
				listeners: {
					blur: function(v){
						var me = this;
						var value = this.getValue();
						var url = me.up("grid").store.orderUrl;
						var record = this.up("grid").getSelectionModel().getLastSelected();
						if(this.regex.test(value)){
							if(gj_onlineLearning_constant.loadType == "jsonp"){
								var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
									loadType: gj_onlineLearning_constant.loadType,
									url: url,
									extraParams: {
										questionnaireQuestionId: record.get("questionnaireQuestionId"),
										orderNum:  value
									}
								});
								store1.load(function(rec, operation, success){
									if(success){
										me.up("grid").store.load();
									}else{
										Orchid.alert('消息', operation.error, function(){
											me.up("grid").store.load();
										});
									}
								});
							}else{
								Ext.Ajax.request({
									url: url,
									params: {
										questionnaireQuestionId: record.get("questionnaireQuestionId"),
										orderNum:  value
									},
									success: function(response){
										var data = response.responseText;
										var json = Ext.decode(data, true);
										if (json.success) {
											me.up("grid").store.load();
										}
										else {
											Orchid.alert('消息', json.msg, function(){
												me.up("grid").store.load();
											});
										}
									}
								});
							}
						}else{
							Orchid.alert('消息','请输入正整数');
						}
					}
				}
			}
        }];
        me.store = Ext.create('PJ.'+ prototype +'.celaquestionnairemanage.IndividualChoiceList', {
            isLoad: false
        });
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
        this.plugins = [Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1,
            listeners: {
                beforeedit: function(editor, e){
                    var combo = e.column.getEditor();
                    if(gj_onlineLearning_constant.loadType == "jsonp"){
                    	var storeToken = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                    		loadType: gj_onlineLearning_constant.loadType,
                    		url: gj_onlineLearning_constant.tokenUrl
                    	});
                    	storeToken.load(function(rec, operation, success){
                    		var token = operation.response;
                    		combo.token = token;
                    	});
                    }else if(gj_onlineLearning_constant.loadType == "ajax"){
                    	Ext.Ajax.request({
                            url: tokenUrl,
                            success: function(response){
                                var data = response.responseText;
                                var token = Ext.decode(data, true);
                                combo.token = token;
                            }
                        });
                    }
                }
            }
        })];
        me.callParent(arguments);
    },
    
    //新增
    onAddClick: function(button){
    	var me = this;
        var win = Ext.create('PJ.view.celabankmanagement.Edit', {
            grid: this,
            isReadOnly:false,
            notEdit: true,
            questionnaireId:me.up("window").questionnaireId,
            title: "新增题目"
        }).show();
        win.down('panel').getForm().findField("questionType").setValue("danxt");
        win.down('panel').getForm().findField("questionType").readOnly = true;
    },
    //删除
    onDeleteClick: function(){
    	var me = this;
        var records = me.getSelectionModel().getSelection();
        Ext.Msg.confirm({
            title: '提示',
            msg: '您确定要删除这些信息吗？',
            buttons: Ext.Msg.OKCANCEL,
            fn: function(id){
                if (id == 'ok') {
                	me.loadMask.show();
                    var questionIds = new Array();
                    Ext.Array.each(records, function(name, index){
                    	questionIds.push(name.get("questionnaireQuestionId"));
                    });
                    if(gj_onlineLearning_constant.loadType == "jsonp"){
	                    var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
	                        url: me.store.deleteUrl,
	                        extraParams: { //请求的相关参数
	                        	questionnaireQuestionIds: questionIds
	                        },
	                        loadType:gj_onlineLearning_constant.loadType//View层的loadType调整，用来切换是否需要以JSONP方式请求。
	                    });
	                   store.load(function(rec, operation, success){
	                	   me.loadMask.hide();
	                         if (success) {//json返回结果中的success是否成功
	                        	 Ext.Msg.alert('消息', '删除成功！', function(){
	                                 var store = me.getStore();
	                                 store.reload();
	                             });
	                         }else {
	                        	 Orchid.alert('消息', operation.error, function(){
	                                 var store = me.getStore();
	                                 store.reload();
	                             });
	                        }
	                   });
                    }else{
                    	Ext.Ajax.request({
	        				url: me.store.deleteUrl,
	        				params: {
	        					questionnaireQuestionIds: questionIds
	        				},
	        				success: function(response){
	        					me.loadMask.hide();
	        					var data = response.responseText;
	        					var json = Ext.decode(data, true);
	        					if (json.success) {
	        						Orchid.alert('消息', '删除成功！', function(){
		                                 var store = me.getStore();
		                                 store.reload();
		                             });
	        					}
	        					else {
	        						Orchid.alert('消息', json.msg||"删除失败!", function(){
	        							 var store = me.getStore();
		                                 store.reload();
	        						});
	        					}
	        				}
	        			});
                    }
                }
                else {
                	me.loadMask.hide();
                    return;
                }
            }
        });
    },
    //从题库选取题目
    onSelectquestionsClick: function(button){
    	var me = this;
    	Ext.create('AM.default.DefaultWindowView', {
            grid: me,
            notEdit: true,
            width: 600,
            height: 380,
            title: "从题库选取题目",
            questionType:"danxt",
            questionnaireId:me.up("window").questionnaireId,
            items: [{
            	xtype: 'celaSelectQuestionsList'
            }],
            buttons: [{
                text: '关闭',
                handler: function(){
                    this.up("window").close();
                }
            }]
        }).show();
    },
    listeners: {
    	viewready: function(){
    		this.addDocked([{
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [this.addButton, "-", {
                    text: '从题库选取题目',
                    iconCls: 'setExamPaperIcon',
                    itemId: this.oldId + "Selectquestions",
                    disabled: false,
                    scope: this,
                    handler: this.onSelectquestionsClick
                }]
            }]);
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    	}
    }
});
